/**
 * File: BatchBucketLoadProcessor.java
 * Created by: mhaimel
 * Created on: 1 Sep 2009
 * CVS:  $Id: BatchBucketLoadProcessor.java,v 1.1 2009/10/09 15:14:58 mhaimel Exp $
 */
package uk.ac.ebi.curtain.processor.writer.impl;

import java.io.File;

import org.apache.commons.lang.NotImplementedException;

import uk.ac.ebi.curtain.processor.AbstractQueueProcessor;
import uk.ac.ebi.curtain.processor.writer.BucketCollectionBean;
import uk.ac.ebi.curtain.utils.io.impl.FileIO;

/**
 * @author mhaimel
 *
 */
public class BatchBucketLoadProcessor extends AbstractQueueProcessor<File, BucketCollectionBean> {

	@Override
	protected BucketCollectionBean buildReturnObject() {
		return null;
	}

	@Override
	public void process() {
		int cnt = 0;
		getLog().debug("Start Index Read/write Batch processing from queue  ...");
		BucketLoadProcessor job = getContext().getInjector().getInstance(BucketLoadProcessor.class);
		try {
			while(true){
				if(isForceShutdown()){
					getLog().warn("FORCE shutdown!");
					break;
				}
				File in = takeInput();
				if(null != in){
					++cnt;
					job.setInput(in);
					job.process();
		//				BucketCollectionBean output = job.buildReturnObject();
					BucketCollectionBean bean = job.buildReturnObject();
					submitOutput(bean);
				} else if(getInputQueue().isShutdown()){
					getLog().debug("Queue is finished!");
					break;
				}
			}
		} catch (InterruptedException e) {
			getLog().warn("Interupted!!!",e);
			Thread.currentThread().interrupt();
		}finally{
			FileIO.closeQuietly(job);
		}
		getLog().info("Finished Index Read/write Batch processing of items: " + cnt);
	}

	@Override
	protected BucketCollectionBean doProcess(File input) {
		throw new NotImplementedException();
	}

	
}
